﻿using System;
using System.IO;
using System.Linq;
using MedicWeb.MedicService;

namespace MedicWeb.forms
{
    public partial class tmpReport : SessionPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var mc = new MainService();

            //STUB
            var order = new Order {Id = 173};
            int year = 2010;
            ////
            Doctor[] doctorList = mc.GetDoctorListByOrder(GetSession(), order);

            var list = (doctorList
                .OrderBy(d => d.NumberInOrder)
                .ThenBy(d => d.GroupExp.Group)
                .ThenBy(d => d.NewClass)
                .ThenBy(d => d.NewSpeciality)
                .ThenBy(d => d.Assign)
                .ThenBy(d => d.Reject)
                .Select(d => new
                                 {
                                     d.Reject,
                                     d.Assign,
                                     newclass = d.NewClass.Title,
                                     newspec = d.NewSpeciality.Title,
                                     d.NumberInOrder,
                                     d.Surname,
                                     d.Name,
                                     d.Patronymic,
                                     job = d.Job.Title,
                                     degree = d.Degree.Title,
                                     institution = d.Institution.Title
                                 })).ToArray();
            var groups = (from l in list
                          group l by new {l.Assign, l.Reject, l.newclass, l.newspec}
                          into sec
                          select
                              new {sec.Key.Reject, sec.Key.Assign, sec.Key.newclass, sec.Key.newspec, count = sec.Count()}).
                ToArray();


            string thtempaltebegin = "<table><tbody>";
            string tr1tempalte =
                "<tr><td colspan=\"3\">&nbsp;</td></tr><tr><td>$paragraf</td><td colspan=\"2\">$action &nbsp $category квалификационную категорию по специальносит $speciality следующим специалистам по списку:</td></tr>";
            string tr2tempalte =
                "<tr><td>$numberinorder</td><td>$surname<br/>$name &nbsp; $patroname <br/>$degree</td><td>$job <br/>$institution</td></tr>";
            string thtempalteend = "</tbody></table>";

            string result = thtempaltebegin;
            var first = true;
            foreach (var group in groups)
            {
                string category = group.newclass;
                string speciality = group.newspec;
                string action = "";


                if (group.Reject == 0 | group.Reject == null)
                {
                    category = category
                        .Replace("первая", "первую")
                        .Replace("вторая", "вторую")
                        .Replace("высшая", "высшую");
                    if (group.Assign == 1)
                    {
                        action = "Присвоить";
                    }
                    else if (group.Assign == 0)
                    {
                        action = "Подтвердить";
                    }
                }
                else
                {
                    category = category
                        .Replace("первая", "первой")
                        .Replace("вторая", "второй")
                        .Replace("высшая", "высшей");
                    if (group.Assign == 1)
                    {
                        action = "Отказать в присвоение";
                    }
                    else if (group.Assign == 0)
                    {
                        action = "Отказать в подтверждение";
                    }
                }
                
                result = result + tr1tempalte.Replace("$action", action)
                                      .Replace("$category", category)
                                      .Replace("$speciality", speciality);
                if (first)
                {
                    result = result.Replace("$paragraf", "II");
                    first = false;
                }
                else
                {
                    result = result.Replace("$paragraf", "");
                }
                var doctors = list.Take(group.count);
                list = list.Skip(group.count).Take(list.Count()).ToArray();
                foreach (var doctor in doctors)
                {
                    var numberinorder = doctor.NumberInOrder + ".";
                    var surname = doctor.Surname;
                    var name = doctor.Name;
                    var patroname = doctor.Patronymic;
                    var degree = doctor.degree != "отсутствует" ? doctor.degree: "";
                    var job = doctor.job;
                    var institution = doctor.institution;

                    result = result + tr2tempalte.Replace("$numberinorder", numberinorder)
                                          .Replace("$surname", surname)
                                          .Replace("$name", name)
                                          .Replace("$patroname", patroname)
                                          .Replace("$degree", degree)
                                          .Replace("$job", job)
                                          .Replace("$institution", institution);
                }
            }
            result = result + thtempalteend;
            GridView1.DataSource = groups;
            GridView1.DataBind();
            Response.Write(result);
        }
    }
}